package com.ccz.demo.design.proxy.logdemo.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * 基于jdk的动态代理
 * @ClassName LogHandler
 * @Author caihaoyun
 * @Date 2020/5/29
 * @Version V1.0
 * 日志代理对象
 **/
public class LogHandler implements InvocationHandler {

    Object target; // 被代理的对象，实际的方法执行者

    public LogHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object result = method.invoke(target, args);
        after();
        return result;  // 返回方法的执行结果
    }

    private void after() {
        System.out.println(String.format("log start time [%s]",new Date()));
    }

    private void before() {
        System.out.println(String.format("log end time [%s]",new Date()));
    }
}
